package controller;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import Pojo.Userlist;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import Pojo.User;
import service.UserService;
import service.UserlistService;

@Controller

public class UserController {

	@Autowired
	private UserService userService;

	@Autowired
	private UserlistService userlistService;
	
	@RequestMapping("/login")
	public String userList() throws Exception{
		
		
		return "login";
		
	}
	@RequestMapping("/updatePwd")
	public String updatePwd() throws Exception{


		return "updatePwd";

	}
	@RequestMapping("/doUpdatePwd")
	public String doUpdatePwd(String username,String password,String password2,String question,Model model,HttpSession httpSession) throws Exception{
		System.out.println("修改密码");
		if ("".equals(username)||username==null) {
			System.out.println("用户名为空");
			String error="error";
			model.addAttribute("error", error);
			return "updatePwd";
		}
		if ("".equals(password)||password==null) {
			System.out.println("新密码为空");
			String error="error";
			model.addAttribute("error", error);
			return "updatePwd";
		}
		if ("".equals(password2)||password2==null) {
			System.out.println("确认密码为空");
			String error="error";
			model.addAttribute("error", error);
			return "updatePwd";
		}
		User user = new User();
		user.setUsername(username);
		User user1 = userService.getoneUser(user);
		if (user1==null||"".equals(question)||question==null){
			System.out.println("问题为空");
			String error="error";
			model.addAttribute("error", error);
			return "updatePwd";
		}

		int r = userService.updatePwd(username, password);

		if(r==1){
			model.addAttribute("error", "ok");
			return "login";
		}else{
			model.addAttribute("error", "error");
			return "updatePwd";
		}
	}
	@RequestMapping("/logincheck")
	public String login(User user,Model model,HttpSession httpSession) throws Exception{
		System.out.println("进行登录");
		User user1=userService.login(user);
		
		if(user1!=null){
			httpSession.setAttribute("user", user1);
			if(user1.getType().equals("zuke")){
				return "zuke/main";
			}
			else{
				return "admin/main1";
			}
		}else{
			String error="error";
			model.addAttribute("error", error);
		return "login";
		}
	}
	@RequestMapping("/toindex")
	public String toindex(Model model) throws Exception{
		
		
		return "admin/index";
		}

	@RequestMapping("/toRegister")
	public String toRegister(){
		System.out.println("进来了");
		return "userRegister";
	}


	@RequestMapping("/doRegister")
	public String doRegister(String username,String password,String truename,String sfzID,String userPhone,String question){

		User u1 = new User();
		Userlist userlist = new Userlist();
		u1.setUsername(username);
		u1.setPassword(password);
		u1.setType("zuke");
		u1.setQuestion(question);
		userService.doRegister(u1);

		System.out.println(u1);
		User user11 = userService.getoneUser(u1);
		System.out.println(user11);
		userlist.setName(truename);
		userlist.setIdcard(sfzID);
		userlist.setPhone(userPhone);
		userlist.setUser_id(user11.getId());
		userlistService.insertuserlist(userlist);
		return "login";
	}

	}

